﻿// JScript File

function GridControl_HandleScroll(oGridControl)
{
    var oHeaderPanel = oGridControl.headerArea;
	var oGridAreaPanel	= oGridControl.gridArea;
	if (!IsNull(oHeaderPanel) && !IsNull(oGridAreaPanel))
	{
		oHeaderPanel.scrollLeft = oGridAreaPanel.scrollLeft;
		
		var marginAdjustment = oHeaderPanel.scrollLeft - oGridAreaPanel.scrollLeft;
		if (IsNull(marginAdjustment))
		{
			return;
		}
		
		marginAdjustment = parseInt(marginAdjustment, 10);

		if (marginAdjustment != parseInt(oHeaderPanel.style.marginLeft,10))
		{
			oHeaderPanel.style.marginLeft = marginAdjustment;
		}
	}
}

function GridControl_ResizeColumns(oGridControl)
{
	var oStyleArray = oGridControl.styleArray;

	if (IsNull(oGridControl.setInitialWidths)) {
		GridControl_SetInitialWidths(oGridControl);
		oGridControl.setInitialWidths = true;
	}
	
	var oCrmGrid		= oGridControl.innerGrid;
	var oHeaderTable	= oGridControl.gridHeader;
	
	if (!IsNull(oCrmGrid) && !IsNull(oHeaderTable))
	{
		if(!IsNull(oCrmGrid.isResizing) && oCrmGrid.isResizing == true)
		{
			return;
		}
		else
		{
			oCrmGrid.isResizing = true;
		}
		
		oCrmGrid.style.tableLayout = "fixed";
	    
		for (var cell = 0 ; cell < oCrmGrid.rows[0].cells.length; cell++)
        {
		    if(IsNull(oCrmGrid.rows[0].cells[cell].widthSpecified))
		    {
		        try
		        {
		            oCrmGrid.rows[0].cells[cell].style.width = "";
		        }
		        catch(e){}
		    }
        }
	    
	    if(oCrmGrid.rows[0].style.display != "inline")
	    {
	        oCrmGrid.rows[0].style.display = "inline";
	    }
		
	    if(oHeaderTable.style.display != "none")
	    {
	        oHeaderTable.style.display = "none";
	    }
	    
	    for (var cell = 0 ; cell < oCrmGrid.rows[0].cells.length; cell++) {
	        if (IsNull(oCrmGrid.rows[0].cells[cell].widthSpecified))
	        {
	            try
	            {
	                if (oCrmGrid.rows[0].cells[cell].offsetWidth <= oCrmGrid.rows[0].cells[cell].preferredWidth)
	                    oCrmGrid.rows[0].cells[cell].style.width = oCrmGrid.rows[0].cells[cell].preferredWidth;
	                else
	                    oCrmGrid.rows[0].cells[cell].style.width = oCrmGrid.rows[0].cells[cell].offsetWidth;
	            }
	            catch (e) { }
	        }
	        else
	        {
	            try
	            {
	                if (oCrmGrid.rows[0].cells[cell].expanded == true)
	                    oCrmGrid.rows[0].cells[cell].style.width = oCrmGrid.rows[0].cells[cell].preferredWidth;
	                else
	                    oCrmGrid.rows[0].cells[cell].style.width = oCrmGrid.rows[0].cells[cell].widthSpecified;
	            }
	            catch (e) { }
	        }
	    }
		
		if(oCrmGrid.rows[0].style.display != "none")
		{
		    try
			{
		        oHeaderTable.style.width = oCrmGrid.offsetWidth;
		    }
			catch(e){}
		    for (var cell = 0 ; cell < oCrmGrid.rows[0].cells.length; cell++)
		    {
			    try
			    {
			        oHeaderTable.rows[0].cells[cell].style.width = oCrmGrid.rows[0].cells[cell].offsetWidth;
			        if (oGridControl.allowSorting == false && IsNull(oHeaderTable.rows[0].cells[cell].sortDisabled)) {
			        	for (var I = 0; I < oHeaderTable.rows[0].cells[cell].children.length; I++) {
			        		var child = oHeaderTable.rows[0].cells[cell].children[I];
			        		if (child.tagName == "A") {
			        			child.href = "javascript:void(0)";
			        		}
			        	}
			        	oHeaderTable.rows[0].cells[cell].sortDisabled = true;	        
					}
			    }
			    catch(e){}
		    }
		    oCrmGrid.rows[0].style.display = "none";
		}
	    
	    oHeaderTable.style.display = "inline";
		oCrmGrid.isResizing = false;
	}
	
	var oHeaderPanelParent = oGridControl.headerArea.parentElement;
	if(!IsNull(oHeaderPanelParent))
	{
	    oHeaderPanelParent.className = oStyleArray[1];
	}
}

function GridControl_SelectRow(rowIndex, setSelected)
{
    var oGridControl = this;
    if(IsNull(setSelected))
    {
        setSelected = true;
    }
    GridControl_SelectRowImpl(oGridControl, rowIndex, setSelected);
	GridControl_UpdateSelections(oGridControl);
}

function GridControl_SingleSelectRow(rowIndex, setSelected)
{
    var oGridControl = this;
    if(IsNull(setSelected))
    {
        setSelected = true;
    }	
    GridControl_SingleSelectRowImpl(oGridControl, rowIndex, setSelected);
	GridControl_UpdateSelections(oGridControl);
}

function GridControl_SelectRowImpl(oGridControl, rowIndex, setSelected)
{
    if(oGridControl.allowClientSideSelection == false)
    {
        throw JsMsg_GridControl_ClientSideSelectionNotEnabled;
    }
    
    var oStyleArray = oGridControl.styleArray;
	
    var oCrmGrid = oGridControl.innerGrid;
    var oRow = oCrmGrid.rows[rowIndex];
    var oCell = oRow.cells[0];
	var oCheckBox = oCell.children[0];
	if(oCheckBox.checked != setSelected) {
		oCheckBox.checked = setSelected;
	}
	if (!IsNull(oGridControl.onClientRowSelectionChanged))
	{
		oGridControl.onClientRowSelectionChanged(oGridControl, rowIndex - 1, setSelected);
	}
	if (setSelected) {
		var cssClass = oStyleArray[4];
		if (!IsNull(oRow.selectedRowCssClass)) {
			cssClass = oRow.selectedRowCssClass;
		}
		oRow.className = cssClass;
	}
	else {
		var cssClass = oStyleArray[3];
		if (!IsNull(oRow.selectedRowCssClass)) {
			cssClass = oRow.rowCssClass;
		}
		oRow.className = cssClass;
	}
}

function GridControl_SingleSelectRowImpl(oGridControl, rowIndex, setSelected)
{
    GridControl_SelectRowImpl(oGridControl, rowIndex, setSelected);
    
    if(setSelected == false)
    {
        return;
    }
    
    var oCrmGrid = oGridControl.innerGrid;
    var selectedRow = rowIndex;
    var oStyleArray = oGridControl.styleArray;
	for (var row = 1 ; row < oCrmGrid.rows.length; row++)
	{
		if(row == selectedRow)
		{
		    continue;
		}
		var oRow = oCrmGrid.rows[row];
		var oCell = oRow.cells[0];
		var oCheckBox = oCell.children[0];
		if(!IsNull(oCheckBox))
		{
			oCheckBox.checked = false;
			var cssClass = oStyleArray[3];
			if (!IsNull(oRow.rowCssClass)) {
				cssClass = oRow.rowCssClass;
			}
			oRow.className = cssClass;
		}
	}
}

function GridControl_HandleDblClick(oGridControl)
{
    var oElement = event.srcElement;
    for(; !IsNull(oElement); oElement = oElement.parentElement)
	{
		if(oElement.nodeName == "TR")
		{
			var parentElement = oElement.parentElement;
			if(!IsNull(parentElement) && parentElement.parentElement == oGridControl.innerGrid)
			{
				break;
			}
		}
	}
	if(oGridControl.allowClientSideSelection)
	{
	    GridControl_SingleSelectRowImpl(oGridControl, oElement.rowIndex, true);
	    GridControl_UpdateSelections(oGridControl);
	}
	return oElement;
}

function GridControl_SelectAll(setSelected)
{
    var oGridControl = this;
    
    if(oGridControl.allowClientSideSelection == false)
    {
        throw JsMsg_GridControl_SelectAllNotSupported;
    }
    
    if(IsNull(setSelected))
    {
        setSelected = true;
    }
    
    if (!IsNull(oGridControl.onClientRowSelectionChanged))
    {
		oGridControl.onClientRowSelectionChanged(oGridControl, -1, setSelected);
	}		
    
	var oStyleArray = oGridControl.styleArray;
	var oCrmGrid = oGridControl.innerGrid;
	
	for (var row = 1 ; row < oCrmGrid.rows.length; row++)
	{
		var oCell = oCrmGrid.rows[row].cells[0];
		var oCheckBox = oCell.children[0];
		if (!IsNull(oGridControl.onClientRowSelectionChanged)) {
			var returnVal = oGridControl.onClientRowSelectionChanged(oGridControl, row - 1, setSelected);
			if (!IsNull(returnVal)) {
				if (returnVal == false)
					continue;
			}
		}
		oCheckBox.checked = setSelected;
		if (setSelected) {
			var oRow = oCrmGrid.rows[row];
			var cssClass = oStyleArray[4];
			if (!IsNull(oRow.selectedRowCssClass)) {
				cssClass = oRow.selectedRowCssClass;
			}
			oRow.className = cssClass;
		}
		else {
			var oRow = oCrmGrid.rows[row];
			var cssClass = oStyleArray[3];
			if (!IsNull(oRow.rowCssClass)) {
				cssClass = oRow.rowCssClass;
			}
			oCrmGrid.rows[row].className = cssClass;
		}
	}
	
	GridControl_UpdateSelections(oGridControl);
}

function GridControl_UpdateSelections(oGridControl)
{
    var oCrmGrid		= oGridControl.innerGrid;
    
	var selections = "";
	var checkSelectAll = true;
	var selectCount = 0;
	
	for (var row = 1 ; row < oCrmGrid.rows.length; row++)
	{
		var oCell = oCrmGrid.rows[row].cells[0];
		var oCheckBox = oCell.children[0];
		if(!IsNull(oCheckBox) && oCheckBox.checked)
		{
			if(selections != "")
			{
				selections += ",";
			}
			selections += (row - 1).toString();
			selectCount++;
		}
		else
		{
			checkSelectAll = false;
		}
	}
	
	var oSelectInfoPanel = oGridControl.selectionInfoArea;	
	
	if(!IsNull(oSelectInfoPanel))
	{
	    var oSelectionInfo = oSelectInfoPanel.children[0];
	
	    if(!IsNull(oSelectionInfo) && !IsNull(oGridControl.selectionInfoFormat) && !IsNull(oGridControl.noSelectionInfoFormat))
	    {
	        var format = (selectCount > 0? oGridControl.selectionInfoFormat : oGridControl.noSelectionInfoFormat);
	        var selectionInfo = formatString(format, oCrmGrid.rows.length - 1, selectCount);	    
		    oSelectionInfo.innerHTML = selectionInfo;
	    }
	}
	
	var selectedIndices = oGridControl.selectedIndices;
	selectedIndices.value = selections;
	
	var oHeaderTable = oGridControl.gridHeader;
	
	var oCell = oHeaderTable.rows[0].cells[0];
	var oCheckBox = oCell.children[0];
	if(!IsNull(oCheckBox))
	oCheckBox.checked = checkSelectAll;
}

function GridControl_GetItem(index) {
	var oCrmGrid = this.innerGrid;
	if (oCrmGrid.rows.length == 0 || index > oCrmGrid.rows.length || index < -1) {
		return null;
	}
	var oRow = oCrmGrid.rows[index + 1];
	return GridControl_GetItemImpl(oRow);
}

function GridControl_GetItemCount() {
	var oCrmGrid = this.innerGrid;
	return oCrmGrid.rows.length - 1;
}

function GridControl_GetItemImpl(oRow)
{
	var oCell = oRow.cells[0];
	var oCheckBox = oCell.children[0];
	var oItem = new Object();
	if (!IsNull(oRow.itemid)) {
		oItem.itemid = oRow.itemid;
	}
	else {
		oItem.itemid = "";
	}
	if (!IsNull(oRow.itemcode)) {
		oItem.itemcode = oRow.itemcode;
	}
	else {
		oItem.itemcode = "";
	}
	if (!IsNull(oRow.itemlabel)) {
		oItem.itemlabel = oRow.itemlabel;
	}
	else {
		oItem.itemlabel = "";
	}
	oItem.itemIndex = oRow.rowIndex - 1;
	oItem.rowIndex = oRow.rowIndex;
	oItem.row = oRow;
	oItem.selected = oCheckBox.checked;
	return oItem;
}

function GridControl_GetSelectedItems()
{
    var oCrmGrid = this.innerGrid;
    var selectedItems = new Array();
    var index = 0;
    for (var row = 1 ; row < oCrmGrid.rows.length; row++)
	{
	    var oRow = oCrmGrid.rows[row];
		var oCell = oRow.cells[0];
		var oCheckBox = oCell.children[0];
		if(oCheckBox.checked)
		{
		    var oItem = GridControl_GetItemImpl(oRow);		    
		    selectedItems[index++] = oItem;
		}
	}
    return selectedItems;
}

function GridControl_SetInitialWidths(oGridControl)
{
    var oCrmGrid = oGridControl.innerGrid;
    var oHeaderTable = oGridControl.gridHeader;
    if (!IsNull(oCrmGrid) && !IsNull(oHeaderTable))
    {
        for (var cell = 0; cell < oCrmGrid.rows[0].cells.length; cell++)
        {
        	var oCell = oCrmGrid.rows[0].cells[cell];
        	oHeaderTable.rows[0].cells[cell].innerHTML = formatString(oHeaderTable.rows[0].cells[cell].innerHTML, oCell.innerHTML);        	
        }
        for (var cell = 0; cell < oCrmGrid.rows[0].cells.length; cell++)
        {
            var specifiedWidth = oCrmGrid.rows[0].cells[cell].style.width;
            if (IsNull(specifiedWidth) || specifiedWidth == "")
            {
                specifiedWidth = oCrmGrid.rows[0].cells[cell].width;
                if (IsNull(specifiedWidth) || specifiedWidth == "")
                {
                    specifiedWidth = null;
                }
            }
            try {
            	var itemsExpanded = oGridControl.itemsExpanded.value;
            	var expandedIndices = itemsExpanded.split(',');
            	            
                var preferredWidth = oCrmGrid.rows[0].cells[cell].offsetWidth + 12;
                if (!IsNull(specifiedWidth))
                {
                	oCrmGrid.rows[0].cells[cell].widthSpecified = specifiedWidth;
                }
                oCrmGrid.rows[0].cells[cell].preferredWidth = preferredWidth;

                var expanded = false;

                for (var I = 0; I < expandedIndices.length; I++) {
                	if (expandedIndices[I] == cell.toString()) {
                		expanded = true;
                	}
                }

                oCrmGrid.rows[0].cells[cell].expanded = expanded;
            }
            catch (e) { }
        }
        oCrmGrid.style.width = "100%";
    }
}

function GridControl_ToggleExpandColumn(colIndex)
{
    var oGridControl = this;
    var oHeaderCell = oGridControl.gridHeader.rows[0].cells[colIndex];
    var cellIndex = colIndex;
    var oCrmGrid = oGridControl.innerGrid;
    if (oGridControl.allowClientSideSelection == true && cellIndex == 0)
    {
        return;
    }
    if(cellIndex == (oCrmGrid.rows[0].cells.length - 1))
    {
        return;
    }
    if(IsNull(oCrmGrid.rows[0].cells[cellIndex].widthSpecified))
    {
        return;
    }

    if (!IsNull(oGridControl.toggleExpand)) {
    	var returnVal = oGridControl.toggleExpand(oGridControl, cellIndex, !oCrmGrid.rows[0].cells[cellIndex].expanded);
    	if (!IsNull(returnVal)) {
    		if (returnVal == false)
    			return;
    	}
    }
    
    if(oCrmGrid.rows[0].cells[cellIndex].expanded == false)
    {
        oCrmGrid.rows[0].cells[cellIndex].style.width = oCrmGrid.rows[0].cells[cellIndex].preferredWidth;
    }
    else
    {
        oCrmGrid.rows[0].cells[cellIndex].style.width = oCrmGrid.rows[0].cells[cellIndex].widthSpecified;
    }

    oCrmGrid.rows[0].cells[cellIndex].expanded = !oCrmGrid.rows[0].cells[cellIndex].expanded;

    var itemsExpanded = "";

    for (var cell = 0; cell < oCrmGrid.rows[0].cells.length; cell++) {
		if(!IsNull(oCrmGrid.rows[0].cells[cell].expanded))
		if (oCrmGrid.rows[0].cells[cell].expanded) {
			if (itemsExpanded != "") {
				itemsExpanded += ",";
			}
			itemsExpanded += cell.toString();
		}
	}

	oGridControl.itemsExpanded.value = itemsExpanded;
	
    oHeaderCell.style.width = oCrmGrid.rows[0].cells[cellIndex].widthSpecified;
    GridControl_ResizeColumns(oGridControl);
    GridControl_HandleScroll(oGridControl);
}

function GridControl_IsColumnExpanded(colIndex)
{
    var oGridControl = this;
    var oCrmGrid = oGridControl.innerGrid;
    var expanded = true;
    if(!IsNull(oCrmGrid.rows[0].cells[colIndex].expanded))
    {
        expanded = oCrmGrid.rows[0].cells[colIndex].expanded;
    }
    return expanded;
}
